package team.scau.laboratory_repair.web.pc;

import com.querydsl.core.QueryResults;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import team.scau.laboratory_repair.common.base.BaseController;
import team.scau.laboratory_repair.common.json.JqGridResult;
import team.scau.laboratory_repair.common.json.PoToDto;
import team.scau.laboratory_repair.modules.dto.RepairClassifyDto;
import team.scau.laboratory_repair.modules.po.Area;
import team.scau.laboratory_repair.modules.po.QRepairClassify;
import team.scau.laboratory_repair.modules.po.RepairClassify;
import team.scau.laboratory_repair.service.AreaService;
import team.scau.laboratory_repair.service.RepairClassifyService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 30254
 * creadtedate:2018/9/16
 */
@Controller
@CommonsLog
@RequestMapping("/pc/biz/classify")
public class RepairClassifyController extends BaseController {

    @Autowired
    private RepairClassifyService repairClassifyService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private PoToDto poToDto;

    @ModelAttribute("classify")
    public RepairClassify get(@RequestParam(required = false, value = "id") Long id) throws Exception {
        if (id != null && id > 0) {
            return repairClassifyService.findById(id);
        } else {
            return new RepairClassify();
        }
    }

    @RequiresPermissions("biz:classify:list")
    @GetMapping(value = "/list", produces = "text/html;charset=utf-8")
    public String list(Model model) throws Exception{
        List<Area> areas = areaService.listOrByExample(null,null).getResults();
        Map<Long, String> areaNameMap = new HashMap<>();
        for(Area area : areas){
            areaNameMap.put(area.getId(), area.getName());
        }
        model.addAttribute("areas", areas);
        model.addAttribute("areaMap", areaNameMap);
        return "/pc/biz/classify/list";
    }

    @ResponseBody
    @RequiresPermissions("biz:classify:list")
    @RequestMapping(value = "/list.json", produces = "application/json;charset=utf-8")
    public JqGridResult list(@ModelAttribute RepairClassify classify) throws Exception {
        QueryResults<RepairClassify> queryResults = repairClassifyService.listOrByExample(classify, getPage(QRepairClassify.class));
        List<RepairClassifyDto> classifies = poToDto.repairClassifyList(queryResults.getResults());
        return new JqGridResult(queryResults, classifies);
    }

    @RequiresPermissions("biz:classify:edit")
    @GetMapping(value = "/edit", produces = "text/html;charset=utf-8")
    public String edit(@ModelAttribute("classify") RepairClassify classify, Model model) throws Exception{
        model.addAttribute("classify",classify);
        QueryResults<Area> areas = areaService.listOrByExample(null,null);
        model.addAttribute("areas", areas.getResults());
        model.addAttribute("clazzes", areas.getResults());
        return "/pc/biz/classify/edit";
    }

    @RequiresPermissions(value = "biz:classify:edit", logical = Logical.OR)
    @PostMapping(value = "/edit", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> edit(@ModelAttribute RepairClassify classify){
        Map<String,Object> result = new HashMap<>();
        try {
            repairClassifyService.save(classify);
            result.put("status",200);
            result.put("msg","分类信息保存成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.put("status",500);
            result.put("msg","分类信息保存失败");
        }
        return result;
    }
    @RequiresPermissions("biz:classify:delete")
    @PostMapping(value = "/delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> delete(Long id){
        Map<String,Object> result = new HashMap<>();
        try {
            if(id != null && id > 0){
                repairClassifyService.deleteById(id);
                result.put("status",200);
                result.put("msg","删除分类成功");
            }else {
                result.put("status",500);
                result.put("msg","请选择删除的分类");
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.put("status",500);
            result.put("msg","删除分类失败");
        }
        return result;
    }
}
